package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.ResponseResult;
import com.woniuxy.entity.Dishinfo;
import com.woniuxy.exception.StoreNotExistException;
import com.woniuxy.mapper.DishinfoMapper;
import com.woniuxy.service.DishinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2021-03-23
 */
@Service
@Slf4j
@SuppressWarnings("all")
public class DishinfoServiceImpl extends ServiceImpl<DishinfoMapper, Dishinfo> implements DishinfoService {
    @Autowired
    private DishinfoMapper dishinfoMapper;
    @Override
    public List<Map<String, Object>> getDishInfosBySid(Integer id) {
        QueryWrapper<Dishinfo> queryWrapper = new QueryWrapper<>();
//        指定查询字段以及条件
        queryWrapper.eq("store_id",id);
        int count = dishinfoMapper.selectCount(queryWrapper);
        if(count ==0){
            throw new StoreNotExistException("该店铺不存在");
        }
        queryWrapper.select("id","dish_name","image");
        List<Map<String, Object>> maps = dishinfoMapper.selectMaps(queryWrapper);
        return maps;
    }

    @Override
    public List<Dishinfo> getDishInfosBySidAndCid(Dishinfo dishinfo) {
        QueryWrapper<Dishinfo> queryWrapper = new QueryWrapper<>();
     queryWrapper.eq(!StringUtils.isEmpty(dishinfo.getCategoryId()),"category_id",dishinfo.getCategoryId()).and(e->{
         e.eq("1","1").eq(!StringUtils.isEmpty(dishinfo.getPrice()),"price",dishinfo.getPrice());
     });
        List<Dishinfo> dishinfos = dishinfoMapper.selectList(queryWrapper);
        return dishinfos;

    }

    @Override
    public Dishinfo getDetailDishInfo(Integer id) {
        QueryWrapper<Dishinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int count = dishinfoMapper.selectCount(queryWrapper);
        if(count ==0){

        }
        Dishinfo dishinfo = dishinfoMapper.selectOne(queryWrapper);

        return dishinfo;
    }
    @Override
    public Integer isTake(Integer id) {
        int i = 0;
        int count = dishinfoMapper.isTake(id);

        return count;
    }

    @Override
    public BigDecimal getPriceById(Integer id) {
           //构建查询添加
        log.debug("菜品id{}号",id);
        QueryWrapper <Dishinfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id).select("price");
        Dishinfo dishinfo = dishinfoMapper.selectOne(queryWrapper);
        log.info("菜品价格{}元",dishinfo.getPrice());
        return dishinfo.getPrice();
    }
}
